﻿
using Domain;
using TianYuan.Core.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ApiDemo.Controllers
{
    
    public class ProductController : ApiController
    {
        private readonly IProductAppService _productAppService;
        public ProductController(IProductAppService productAppService)
        {
            _productAppService = productAppService;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IHttpActionResult GetAll()
        {
            var list = _productAppService.GetProducts();
            
            return Json<List<Product>>(list);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        
        [HttpPost]
        public IHttpActionResult Add([FromBody]ProductDto dto)
        {
            //var p = new ProductDto
            //{
            //    Id = Guid.NewGuid().ToString(),
            //    Name = "曾繁林",
            //    Price = 2,
            //};
            var result = _productAppService.Add(dto);
            return Json<bool>(result);
        }
        /// <summary>
        /// AOP缓存测试
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpGet]
        [Route("product/query")]
        public IHttpActionResult Query()
        {
            var p = new ProductQueryDto
            {
                Id = "11111",
                Name = "曾繁林",
                
            };
            var result = _productAppService.GetTestCache(p);
            return Json(result);
        }
        /// <summary>
        /// 清缓存
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult ClearCache([FromBody]ProductQueryDto dto)
        {
            //var p = new ProductDto
            //{
            //    Id = Guid.NewGuid().ToString(),
            //    Name = "曾繁林",
            //    Price = 2,
            //};
             _productAppService.ClearCache(dto);
            return Ok();
        }
        [HttpGet]
        [Route("product/filter")]
        public IHttpActionResult TestFilter()
        {
            _productAppService.TestFilters();
            return Ok();
        }
    }
}
